package com.bbs.toutiao.controller.admin;

import cn.hutool.core.collection.ListUtil;
import com.bbs.toutiao.service.admin.impl.AdminHomeServiceImpl;
import com.bbs.toutiao.utils.ChatUtil;
import com.bbs.toutiao.utils.Result;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;
import java.util.List;
import java.util.Map;

/**
 * 描述: 管理系统home页面
 * 作者： zhangBin
 * 时间： 2024/3/5 20:27
 * 版本： 1.0
 */

@RestController
@RequestMapping("/admin/home")
public class AdminHomeController {

    @Resource
    AdminHomeServiceImpl adminHomeService;

    @Resource
    ChatUtil chatUtil;

    /**
     * 统计每日注册人数
     *
     * @return
     */
    @GetMapping("/selectRegisterCount")
    public Result selectRegisterCount() {

        return Result.success("查询成功", adminHomeService.registerCounts());
    }


    @GetMapping("/selectArticleClassifyCount")
    public Result selectArticleClassifySum() {
        return Result.success("查询成功", adminHomeService.articleClassifyCount());
    }

    //sse查询在线人数
    @GetMapping(value = "/onlineCount", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public SseEmitter onlineCount(@PathParam("userId") Integer userId) {

        return chatUtil.createAdminSse(userId.toString());
    }

    //统计日常评论数量
    @GetMapping("/selectCommentCount")
    public Result selectCommentCount() {
        List<Map<String, String>> maps = adminHomeService.commentCount();
        ListUtil.reverse(maps);
        return Result.success("查询成功", maps);
    }
}
